package com.liyou.auth.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.liyou.auth.service.SysRoleService;
import com.liyou.common.result.Result;
import com.liyou.model.system.SysRole;
import com.liyou.vo.system.AssginRoleVo;
import com.liyou.vo.system.SysRoleQueryVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;

@Api(tags = "角色管理接口")
@RestController
@RequestMapping("/admin/system/sysRole")
public class SysRoleController {
    @Resource
    private SysRoleService sysRoleService;

    //1 查询所有角色 和 当前用户所属角色
    @ApiOperation("获取角色")
    @GetMapping("/toAssign/{userId}")
    public Result toAssign(@PathVariable Long userId) {
        Map<String,Object> map = sysRoleService.findRoleDataByUserId(userId);
        return Result.ok(map);
    }

    //2 为用户分配角色
    @ApiOperation("为用户分配角色")
    @PostMapping("/doAssign")
    public Result doAssign(@RequestBody AssginRoleVo assginRoleVo) {
        sysRoleService.doAssign(assginRoleVo);
        return Result.ok();
    }

    //查询所有角色
    /*@ApiOperation("查询所有的角色")
    @GetMapping("/findAll")
    public Result findAll() {
        List<SysRole> sysRoleList = sysRoleService.list();
        return Result.ok(sysRoleList);
    }*/

    //条件分页查询
    @PreAuthorize("hasAnyAuthority('bnt.sysRole.list')")
    @ApiOperation("条件分页查询")
    @GetMapping("{pageNum}/{pageSize}")
    public Result pageQuery(@PathVariable Long pageNum,
                            @PathVariable Long pageSize,
                            SysRoleQueryVo sysRoleQueryVo) {
        //创建Page对象
        Page<SysRole> sysRolePage = new Page<>(pageNum, pageSize);

        //条件判断
        LambdaQueryWrapper<SysRole> queryWrapper = new LambdaQueryWrapper<>();
        String roleName = sysRoleQueryVo.getRoleName();
        if (!StringUtils.isEmpty(roleName)) {
            queryWrapper.like(SysRole::getRoleName, roleName);
        }

        //调用方法实现
        sysRoleService.page(sysRolePage, queryWrapper);
        return Result.ok(sysRolePage);
    }

    //增加角色
    @PreAuthorize("hasAnyAuthority('bnt.sysRole.add')")
    @ApiOperation("增加角色")
    @PostMapping("add")
    public Result addRole(@RequestBody SysRole sysRole) {
        boolean flag = sysRoleService.save(sysRole);
        if (flag) {
            return Result.ok();
        }
        return Result.fail();
    }

    //查询角色
    @PreAuthorize("hasAnyAuthority('bnt.sysRole.list')")
    @ApiOperation("根据id查询角色")
    @GetMapping("get/{id}")
    public Result getRole(@PathVariable Long id) {
        SysRole sysRole = sysRoleService.getById(id);
        if (sysRole != null) {
            return Result.ok(sysRole);
        }
        return Result.fail();
    }

    //修改角色
    @PreAuthorize("hasAnyAuthority('bnt.sysRole.update')")
    @ApiOperation("修改角色")
    @PutMapping("update")
    public Result updateRole(@RequestBody SysRole sysRole) {
        boolean flag = sysRoleService.updateById(sysRole);
        if (flag) {
            return Result.ok();
        }
        return Result.fail();
    }

    //根据id删除角色
    @PreAuthorize("hasAnyAuthority('bnt.sysRole.remove')")
    @ApiOperation("根据id删除角色")
    @DeleteMapping("delete/{id}")
    public Result deleteRole(@PathVariable Long id) {
        boolean flag = sysRoleService.removeById(id);
        if (flag) {
            return Result.ok();
        }
        return Result.fail();
    }

    //批量删除角色
    @PreAuthorize("hasAnyAuthority('bnt.sysRole.remove')")
    @ApiOperation("批量删除角色")
    @DeleteMapping("batchDelete")
    public Result batchDeleteRole(@RequestBody List<Long> idList) {
        boolean flag = sysRoleService.removeByIds(idList);
        if (flag) {
            return Result.ok();
        }
        return Result.fail();
    }

}
